package com.jemmyee.pizza.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.Prepaid;
import com.jemmyee.pizza.entity.PrepaidVip;
import com.jemmyee.pizza.service.IPrepaidVipService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.PrepaidBean;

@Controller
@RequestMapping("/admin/prepaidVip")
public class PrepaidVipController {

	private static Log log = LogFactory.getLog(PrepaidVipController.class);
	@Autowired
	private IPrepaidVipService<PrepaidVip> prepaidVipService ;
	
	@RequestMapping("/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute PrepaidBean bean,HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(PrepaidVip.class);
		PrepaidBean prepaidBean = new PrepaidBean();
		if (from.equals("form")) {
			prepaidBean = bean;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, prepaidBean);
		}
		if (from.equals("page")) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				prepaidBean = (PrepaidBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
		}
		if (from.equals("menu")) {
			System.out.println("menu is call");
		}

		if(prepaidBean.getName() != null && !prepaidBean.getName().equals("")){
			dc.add(Restrictions.like("prepaidName", prepaidBean.getName(), MatchMode.ANYWHERE));
		}
		
		if(prepaidBean.getNumber() != null && !prepaidBean.getNumber().equals("")){
			dc.add(Restrictions.like("prepaidId", prepaidBean.getNumber(), MatchMode.ANYWHERE));
		}
		
		if(prepaidBean.getAddr() != null && !prepaidBean.getAddr().equals("")){
			dc.add(Restrictions.like("prepaidAddress", prepaidBean.getAddr(), MatchMode.ANYWHERE));
		}
		// 发布时间
		if (prepaidBean.getStartTime() != null) {
			dc.add(Restrictions.ge("createTime", prepaidBean.getStartTime()));
		}
		if (prepaidBean.getEndTime() != null) {
			dc.add(Restrictions.le("createTime", prepaidBean.getEndTime()));
		}

//		dc.addOrder(Order.desc("createTime"));
		ModelAndView mv = new ModelAndView();
		Page page = prepaidVipService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"createTime");
		mv.addObject("page", page);
		mv.addObject("prepaidBean", prepaidBean);
		mv.setViewName("/admin/prepaidVip/list");
		return mv;
	}
	
	@RequestMapping("/view/{id}")
	public ModelAndView view(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		PrepaidVip prepaidVip = prepaidVipService.findById(id);
		mv.setViewName("/admin/prepaidVip/view");
		mv.addObject("prepaid",prepaidVip);
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id){
	   ModelAndView mv=new ModelAndView();
	   PrepaidVip prepaidVip = prepaidVipService.findById(id);
	   mv.setViewName("/admin/prepaidVip/edit");
	   mv.addObject("prepaid",prepaidVip);
	   return mv;
	}
	
	
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody PrepaidVip prepaid,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		PrepaidVip p = prepaidVipService.findById(prepaid.getPrepaidId());
		if(p == null){
			model.put("msg","error");
		}else{
			p.setPrepaidName(prepaid.getPrepaidName());
			p.setMeterName(prepaid.getMeterName());
			p.setPrepaidType(prepaid.getPrepaidType());
			p.setPrepaidPrice(prepaid.getPrepaidPrice());
			p.setPrepaidAddress(prepaid.getPrepaidAddress());
			p.setPrepaidTel(prepaid.getPrepaidTel());
			p.setRemark(prepaid.getRemark());
			prepaidVipService.update(p);
			model.put("msg","succ");
		}
		return model;
	}
	
	@RequestMapping("/to_delete/{id}")
	public ModelAndView toDelete(@PathVariable Integer id){
		return null;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestBody PrepaidVip prepaid,HttpServletRequest request)
	{   
		return null;
	}
	
	
}
